<template>
  <div :class="className"
       :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from "echarts"

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    headTitle: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "350px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    },
    chartTime: {
      type: Array,
      required: true
    },
    min: {
      type: String,
      required: true
    },
    max: {
      type: String,
      required: true
    },
    interval: {
      type: Number,
      required: true
    },
    unit: {
      type: String,
      default: ""
    },
    color: {
      type: String,
      default: "#239DD2"
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(
          this.headTitle,
          this.chartData,
          this.chartTime,
          this.min,
          this.max,
          this.interval,
          this.unit
        );
      }
    },
    chartTime: {
      deep: true,
      handler(val) {
        this.setOptions(
          this.headTitle,
          this.chartData,
          this.chartTime,
          this.min,
          this.max,
          this.interval,
          this.unit
        );
      }
    }
  },
  mounted() {
    window.addEventListener("resize", this.resizeHandler);
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeHandler);
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    resizeHandler() {
      this.chart.resize();
    },
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(
        this.headTitle,
        this.chartData,
        this.chartTime,
        this.min,
        this.max,
        this.interval,
        this.unit
      );
    },
    setOptions(title, chartData, chartTime, min, max, interval, unit) {
      this.chart.setOption({
        title: {
          text: title,
          left: "center"
        },
        grid: {
          left: 20,
          right: 35,
          bottom: 10,
          top: 35,
          containLabel: true
        },
        xAxis: {
          data: chartTime,
          boundaryGap: false, //falseX轴两边对齐
          //坐标轴刻度相关设置
          axisTick: {
            //是否显示坐标轴刻度
            show: false
          },
        },
        yAxis: {
          type: "value",
          name: "单位(" + unit + ")",
          nameLocation: "end",
          min: min,
          max: max,
          //interval: interval,
          boundaryGap: false,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: true
          },
          /*axisLabel: {
            formatter: '{value} ' + unit
          }*/
          axisLabel: {
            formatter: function (value) {
              return parseInt(value)
            }
          }
        },
        //提示框组件
        tooltip: {
          //坐标轴触发，主要在柱状图，折线图等会使用类目轴的图表中使用
          trigger: "axis",
          //坐标轴指示器配置项
          axisPointer: {
            //十字准星指示器
            type: "cross",//shadow阴影效果
            axis: 'y',
            //坐标轴指示器的文本标签
            label: {
              //文本标签中数值的小数点精度
              precision: 0
            }
          },
          formatter: '{b0}<br />' + title + '：{c0}' + unit,
          padding: [5, 10]
        },
        series: [
          {
            lineStyle: {
              shadowColor: this.color,
              // shadowOffsetX: 10,
              color: this.color,
              shadowOffsetY: 4,
              opacity: 1,
              shadowBlur: 10,
              width: 4
            },
            // itemStyle: {
            //   normal: {
            //     color: this.color,
            //     borderColor: this.color,
            //     lineStyle: {
            //       color: this.color,
            //       width: 2
            //     },
            //     label: {
            //       show: false,  //隐藏线点文字
            //       color: "#000"
            //     }
            //   }
            // },
            //name: title,
            smooth: true,     // 这个可以显示为平滑过渡的线条
            symbol: "circle", // 这个把节点的空心圆换成实心圆
            symbolSize: 1,    // 节点大小，数字随意，跟下面的线宽一样或少 1 像素
            type: "line",
            data: chartData,
            animationDuration: 2800,
            animationEasing: "cubicInOut"
          }
        ]
      });

    }
  }
};
</script>
